package com.hsgene.common.util.check;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 对象属性判空工具类
 *
 * @author wxf
 * @date 2018/10/19 14:39
 **/
public class ObjectAttrUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * Log
     */
    private static Logger LOGGER = LogManager.getLogger(ObjectAttrUtils.class);

    /**
     * 判断对象中某些属性是否为空
     *
     * @param obj           对象
     * @param excludeFields 排除字段
     * @return
     */
    public static String checkObjectAttrIsNull(Object obj, List<Object> excludeFields) {
        String result = null;
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String fieldName = field.getName();
                if (excludeFields != null && excludeFields.contains(fieldName)) {
                    continue;
                }
                if (field.getType().toString().equals("class java.lang.String")) {
                    String value = (String) field.get(obj);
                    if (StringUtils.isEmpty(value)) {
                        result = fieldName;
                        return result;
                    }
                }
                if (field.getType().toString().equals("class java.util.List")) {
                    continue;
                }
                if (field.get(obj) == null) {
                    result = fieldName;
                    return result;
                }
                if (field.getType().toString().equals("class java.lang.Integer")) {
                    Integer value = (Integer) field.get(obj);
                    if (value == 0) {
                        result = fieldName;
                        return result;
                    }
                }
                if (field.getType().toString().equals("class java.lang.Double") ||
                        field.getType().toString().equals("double")) {
                    Double value = (Double) field.get(obj);
                    if (value == 0) {
                        result = fieldName;
                        return result;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "error";
        }
        return result;
    }

    /**
     * 获取不必要判断的字段
     *
     * @param objects
     * @return
     */
    public static List<Object> getExcludeObjectFields(Object... objects) {
        List<Object> list = new ArrayList<>();
        for (Object object : objects) {
            list.add(object);
        }
        return list;
    }

    /**
     * 校验参数值
     *
     * @param basicMaps
     * @param object
     * @param excludeStr
     * @return
     */
    public static String setFieldValue(Map<String, Object> basicMaps, Object object, String... excludeStr) {
        try {
            List<Object> excludeFields = excludeStr == null ? new ArrayList<>() : Arrays.asList(excludeStr);
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String fieldName = field.getName();
                Object o = basicMaps.get(fieldName);
                if (excludeFields.contains(fieldName)) {
                    continue;
                }
                if (o == null) {
                    return fieldName;
                }
                if (fieldName.toLowerCase().contains("time")) {
                    field.set(object, sdf.parse((String) o));
                } else if (field.getType().toString().equals("class java.math.BigDecimal") && (o instanceof
                        Integer||o instanceof Double)) {
                    field.set(object, getTowCommitNum(String.valueOf(o)));
                } else {
                    field.set(object, o);
                }
            }
        } catch (Exception e) {
            LOGGER.error("reflect analysis is error...", e);
        }
        return null;
    }


    /**
     * 保留2位小数
     *
     * @param value
     * @return
     */
    public static BigDecimal getTowCommitNum(String value) {
        BigDecimal bg = new BigDecimal(value);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * objectAttrToNameValuePair
     *
     * @param o
     * @return
     */
    public static List<NameValuePair> objectAttrToNameValuePair(Object o, String... excludeField) {
        try {
            if (o == null) {
                return null;
            }
            List<String> list = Arrays.asList(excludeField);
            Field[] fields = o.getClass().getDeclaredFields();
            List<NameValuePair> valuePairs = new ArrayList<>();
            for (Field field : fields) {
                field.setAccessible(true);
                if (list.contains(field.getName())) {
                    continue;
                }
                NameValuePair nameValuePair = new BasicNameValuePair(field.getName(), String.valueOf(field.get(o)));
                valuePairs.add(nameValuePair);
            }
            return valuePairs;
        } catch (Exception e) {
            LOGGER.error("objectAttr To NameValuePair is error...", e);
            return null;
        }
    }
}
